package com.sreader.sreader4j;

import java.util.Locale;

import org.mortbay.jetty.LocalConnector;

import com.google.gwt.user.client.ui.Label;
import com.sreader.sreader4j.FeuilleDeCalcul;
import com.vaadin.Application;
import com.vaadin.service.ApplicationContext;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;

public class Sreader4jApplication extends Application {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4385622315630891808L;
	private Locale defaultLang ;
	private GridLayout mainLayout;
	private Link link_us;
	private Link link_fr;
	private Link link_de;
	private Link link_ch;
	private Link link_at;
	private Link link_it;
	private Link link_es;
	private Link link_pt;
	private Link link_tr;
	private Link link_be;
	private Link link_uk;
	
	private Panel MainPanel;
	private String mainWindowName;
	final Window  mainWindowFr  = new Window("Sreader4u Application Fr");
	final Window  mainWindowDe  = new Window("Sreader4u Application De");
	final Window  mainWindowUs  = new Window("Sreader4u Application Us");
	final Window mainWindowCh   = new Window("Sreader4u Application Ch");
	final Window mainWindowAt   = new Window("Sreader4u Application At");
	final Window mainWindowIt   = new Window("Sreader4u Application It");
	final Window mainWindowEs   = new Window("Sreader4u Application Es");
	final Window mainWindowTr   = new Window("Sreader4u Application Tr");
	final Window mainWindowPt   = new Window("Sreader4u Application Pt");
	final Window mainWindowBe   = new Window("Sreader4u Application Be");
	final Window mainWindowUk   = new Window("Sreader4u Application Uk");
	final Window mainWindow = new Window("Sreader4u Application");
	final Window newWindowIndividuel = new Window();
	final Window newWindowCroise = new Window();

	private FeuilleDeCalculCroise  feuilleCroise;
	
	private static Locale currentLocaleValue;
	private static Messages messages;
	
	private ApplicationMenuBar mainMenuBar;
	private String actualWindowUrl;

	
	private GridLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new GridLayout();
		mainLayout.setImmediate(false);
		//mainLayout.setWidth("100%");
		//mainLayout.setHeight("270px");
		mainLayout.setMargin(false);
		mainLayout.setColumns(4);
		mainLayout.setRows(3);
		
		// top-level component properties
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		
		// link_de
		link_de = new Link();
		link_de = new Link(null,
				new ExternalResource(mainWindowDe.getURL()));
		link_de.setIcon(new ThemeResource("img/de.png"));
		link_de.setDescription("klicke mich für deutsch");
		link_de.setImmediate(false);
		link_de.setWidth("-1px");
		link_de.setHeight("-1px");
		//link_de.setStyleName("mylinkstyle");
		//link_de.setTargetName("mainWindowName");
		mainLayout.addComponent(link_de, 0, 0);
		mainLayout.setComponentAlignment(link_de, Alignment.MIDDLE_CENTER);
		
		// link_fr
		link_fr = new Link(null,
				new ExternalResource(mainWindowFr.getURL()));
		link_fr.setIcon(new ThemeResource("img/fr.png"));
		link_fr.setDescription("clique moi pour le francais");
		link_fr.setImmediate(true);
		link_fr.setWidth("-1px");
		link_fr.setHeight("-1px");
		//link_fr.setTargetName("mainWindowName");
		mainLayout.addComponent(link_fr, 1, 0);
		mainLayout.setComponentAlignment(link_fr, Alignment.MIDDLE_CENTER);	
		
		// link_us
		link_us = new Link(null,
				new ExternalResource(mainWindowUs.getURL()));
		link_us.setIcon(new ThemeResource("img/us.png"));
		link_us.setDescription("Clic me for english");
		link_us.setImmediate(false);
		link_us.setWidth("-1px");
		link_us.setHeight("-1px");
		mainLayout.addComponent(link_us, 2, 0);
		mainLayout.setComponentAlignment(link_us, Alignment.MIDDLE_CENTER);
		
		// link_ch
		link_ch = new Link(null,
				new ExternalResource(mainWindowCh.getURL()));
		link_ch.setIcon(new ThemeResource("img/ch.png"));
		link_ch.setDescription("Clic me for Swiss");
		link_ch.setImmediate(false);
		link_ch.setWidth("-1px");
		link_ch.setHeight("-1px");
		mainLayout.addComponent(link_ch, 3, 0);
		mainLayout.setComponentAlignment(link_ch, Alignment.MIDDLE_CENTER);
		
		// link_at
		link_at = new Link(null,
				new ExternalResource(mainWindowAt.getURL()));
		link_at.setIcon(new ThemeResource("img/at.png"));
		link_at.setDescription("Clic me for Austria");
		link_at.setImmediate(false);
		link_at.setWidth("-1px");
		link_at.setHeight("-1px");
		mainLayout.addComponent(link_at, 0, 1);
		mainLayout.setComponentAlignment(link_at, Alignment.MIDDLE_CENTER);
		
		// link_it
		link_it = new Link(null,
				new ExternalResource(mainWindowIt.getURL()));
		link_it.setIcon(new ThemeResource("img/it.png"));
		link_it.setDescription("Clic me for Italian");
		link_it.setImmediate(false);
		link_it.setWidth("-1px");
		link_it.setHeight("-1px");
		mainLayout.addComponent(link_it, 1, 1);
		mainLayout.setComponentAlignment(link_it, Alignment.MIDDLE_CENTER);
		
		// link_es
		link_es = new Link(null,
				new ExternalResource(mainWindowEs.getURL()));
		link_es.setIcon(new ThemeResource("img/es.png"));
		link_es.setDescription("Clic me for Spain");
		link_es.setImmediate(false);
		link_es.setWidth("-1px");
		link_es.setHeight("-1px");
		mainLayout.addComponent(link_es, 2, 1);
		mainLayout.setComponentAlignment(link_es, Alignment.MIDDLE_CENTER);
		
		// link_pt
		link_pt = new Link(null,
				new ExternalResource(mainWindowPt.getURL()));
		link_pt.setIcon(new ThemeResource("img/pt.png"));
		link_pt.setDescription("Clic me for Portugal");
		link_pt.setImmediate(false);
		link_pt.setWidth("-1px");
		link_pt.setHeight("-1px");
		mainLayout.addComponent(link_pt, 3, 1);
		mainLayout.setComponentAlignment(link_pt, Alignment.MIDDLE_CENTER);
		
		// link_tr
		link_tr = new Link(null,
				new ExternalResource(mainWindowTr.getURL()));
		link_tr.setIcon(new ThemeResource("img/tr.png"));
		link_tr.setDescription("Clic me for Türkey");
		link_tr.setImmediate(true);
		link_tr.setWidth("-1px");
		link_tr.setHeight("-1px");
		mainLayout.addComponent(link_tr, 0, 2);
		mainLayout.setComponentAlignment(link_tr, Alignment.MIDDLE_CENTER);
		
		// link_be
		link_be = new Link(null,
				new ExternalResource(mainWindowBe.getURL()));
		link_be.setIcon(new ThemeResource("img/be.png"));
		link_be.setDescription("Clique moi pour la belgique");
		link_be.setImmediate(true);
		link_be.setWidth("-1px");
		link_be.setHeight("-1px");
		mainLayout.addComponent(link_be, 1, 2);
		mainLayout.setComponentAlignment(link_be, Alignment.MIDDLE_CENTER);
		
		// link_uk
		link_uk = new Link(null,
				new ExternalResource(mainWindowUk.getURL()));
		link_uk.setIcon(new ThemeResource("img/uk.png"));
		link_uk.setDescription("Clic me for United Kingdom");
		link_uk.setImmediate(true);
		link_uk.setWidth("-1px");
		link_uk.setHeight("-1px");
		mainLayout.addComponent(link_uk, 2, 2);
		mainLayout.setComponentAlignment(link_uk, Alignment.MIDDLE_CENTER);
						
		return mainLayout;
	}
	public Locale getDefaultLang() {
		return defaultLang;
	}
	public void setDefaultLang(Locale defaultLang) {
		this.defaultLang = defaultLang;
	}
	
	@Override
	public Window getWindow(String name){
		Window mainWindow     = new Window("Sreader4u Application ");

		AbsoluteLayout mainWindowAbsolutLayout = new AbsoluteLayout();
		mainWindowAbsolutLayout.setWidth("750.0px");
		mainWindowAbsolutLayout.setHeight("950.0px");

		if( super.getWindow(name) == null ){
			 if(name.startsWith("fr-")){
				 setCurrentLocale(new Locale("fr","FR"));
				 mainWindow = mainWindowFr;
			 }else if(name.startsWith("de-")){
				 setCurrentLocale(new Locale("de","DE"));
				 mainWindow = mainWindowDe;
			 }else if(name.startsWith("en-")){
				 setCurrentLocale(new Locale("en", "US"));
				 mainWindow = mainWindowUs;
			 }
			int windowTypeNumber = getWindowTypeNumber(name, getCurrentLocale());
			switch (windowTypeNumber){
			/*case 3:
				newWindowIndividuel.setName(name);
				FeuilleDeCalculIndividuelle feuilleDecalculIndividuelle = new FeuilleDeCalculIndividuelle(getCurrentLocale(), getMainWindow());
				newWindowIndividuel.setCaption("");
				mainWindowAbsolutLayout.addComponent(feuilleDecalculIndividuelle, "top:10.0px;left:10.0px;");
				newWindowIndividuel.setContent(mainWindowAbsolutLayout);
				newWindowIndividuel.setModal(true);
				mainWindow.addWindow(newWindowIndividuel);
				
				//mainWindow.executeJavaScript("window.open('" + newWindowIndividuel.getURL() +"', '_top')");
				mainWindow.open(new ExternalResource(newWindowIndividuel.getURL(), "_top"));
				return newWindowIndividuel;*/
			case 3:
				newWindowCroise.setName(name);
				FeuilleDeCalculCroise feuilleAnalyseCroise = new FeuilleDeCalculCroise(getCurrentLocale(), getMainWindow());		
				mainWindowAbsolutLayout.addComponent(feuilleAnalyseCroise, "top:0.0px;left:10.0px;");
				newWindowCroise.setContent(mainWindowAbsolutLayout);
				newWindowCroise.setModal(true);
				mainWindow.addWindow(newWindowCroise);
				//mainWindow.executeJavaScript("window.open('" + newWindowCroise.getURL() +"', '_top')");
				mainWindow.open(new ExternalResource(newWindowCroise.getURL(), "_top"));
				return newWindowCroise;
			}
			
		}		

		return super.getWindow(name);
	}
	
	@Override
	public void init() {
		
		//mainWindow.setScrollable(true);
		setMainWindow(mainWindow);
		mainWindowName = mainWindow.getName();
		setTheme("sReader4J");
		
		Locale localeFrance = new Locale("fr","FR");
		setCurrentLocale(localeFrance);
		mainWindowFr.setName("fr");
		mainWindowFr.setScrollable(true);
		FeuilleDeCalcul maFeuilleFr = new FeuilleDeCalcul(localeFrance,mainWindowFr);
		
		AbsoluteLayout mainWindowAbsolutLayoutFr = new AbsoluteLayout();
		mainWindowAbsolutLayoutFr.setWidth("750.0px");
		mainWindowAbsolutLayoutFr.setHeight("950.0px");
		
		mainMenuBar= new ApplicationMenuBar(localeFrance, mainWindowFr);
		
		mainWindowAbsolutLayoutFr.addComponent(mainMenuBar.getApplicationMenuBar(), "top:0.0px;left:10.0px;");
		mainWindowAbsolutLayoutFr.addComponent(maFeuilleFr, "top:10.0px;left:10.0px;");	
		//final Window myWindow = new Window("MyWidow");
		//myWindow.setName("myWindow");
		
		//start analyse croisée
		/*final Window newWindow = new Window("Sreader4u Application Fr");
		newWindow.setName("feuilleCroise");
		AbsoluteLayout mainWindowAbsolutLayout = new AbsoluteLayout();
		mainWindowAbsolutLayout.setWidth("750.0px");
		mainWindowAbsolutLayout.setHeight("950.0px");
		feuilleCroise = new FeuilleDeCalculCroise(localeFrance, mainWindowFr);
		mainWindowAbsolutLayout.addComponent(feuilleCroise, "top:10.0px;left:10.0px;");*/
		
		//newWindow.setContent(mainWindowAbsolutLayout);
		//addWindow(newWindow);
		//addWindow(myWindow);
		/*mainWindowFr.addComponent(new Button("foo", new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				mainWindowFr.open(new ExternalResource(newWindow.getURL(), "_top"));
			}
		}));*/
		
		mainWindowFr.setContent(mainWindowAbsolutLayoutFr);
		addWindow(mainWindowFr);
			
		mainWindowDe.setName("de");
		mainWindowDe.setScrollable(true);
		Locale localeDeutschland = new Locale("de","DE");
		setCurrentLocale(localeDeutschland);
		FeuilleDeCalcul maFeuilleDe = new FeuilleDeCalcul(localeDeutschland,mainWindowDe);
		AbsoluteLayout mainWindowAbsolutLayoutDe = new AbsoluteLayout();
		mainWindowAbsolutLayoutDe.setWidth("750.0px");
		mainWindowAbsolutLayoutDe.setHeight("950.0px");
		
		mainMenuBar= new ApplicationMenuBar(localeDeutschland,mainWindowDe);
		mainWindowAbsolutLayoutDe.addComponent(mainMenuBar.getApplicationMenuBar(), "top:0.0px;left:10.0px;");
		
		mainWindowAbsolutLayoutDe.addComponent(maFeuilleDe, "top:10.0px;left:10.0px;");		
		mainWindowDe.setContent(mainWindowAbsolutLayoutDe);
		addWindow(mainWindowDe);
		
		mainWindowUs.setName("en");
		Locale localeUSA = new Locale("en","US");
		FeuilleDeCalcul maFeuilleEn = new FeuilleDeCalcul(localeUSA,mainWindowUs);
		AbsoluteLayout mainWindowAbsolutLayoutEn = new AbsoluteLayout();
		mainWindowAbsolutLayoutEn.setWidth("750.0px");//
		mainWindowAbsolutLayoutEn.setHeight("950.0px");//
		ApplicationMenuBar applicationMenuBarUs= new ApplicationMenuBar(localeUSA,mainWindowUs);
		mainWindowAbsolutLayoutEn.addComponent(applicationMenuBarUs.getApplicationMenuBar(), "top:0.0px;left:10.0px;");
		mainWindowUs.setScrollable(true);
		mainWindowAbsolutLayoutEn.addComponent(maFeuilleEn, "top:10.0px;left:10.0px;");
		mainWindowUs.setContent(mainWindowAbsolutLayoutEn);
		mainWindowUs.getContent().setSizeFull();
		addWindow(mainWindowUs);
		
		mainWindowCh.setName("ch");
		Locale localeSwitzerland = new Locale("de","CH");
		FeuilleDeCalcul maFeuilleCh = new FeuilleDeCalcul(localeSwitzerland,mainWindowCh);//"de_CH"
		AbsoluteLayout mainWindowAbsolutLayoutCh = new AbsoluteLayout();
		mainWindowAbsolutLayoutCh.setWidth("750.0px");//
		mainWindowAbsolutLayoutCh.setHeight("950.0px");//
		ApplicationMenuBar applicationMenuBarCh= new ApplicationMenuBar(localeSwitzerland,mainWindowCh);
		mainWindowAbsolutLayoutCh.addComponent(applicationMenuBarCh.getApplicationMenuBar(), "top:0.0px;left:10.0px;");
		mainWindowCh.setScrollable(true);
		mainWindowAbsolutLayoutCh.addComponent(maFeuilleCh, "top:10.0px;left:10.0px;");		
		mainWindowCh.setContent(mainWindowAbsolutLayoutCh);
		addWindow(mainWindowCh);
		
		mainWindowAt.setName("at");
		Locale localeAustria = new Locale("de","AT");
		FeuilleDeCalcul maFeuilleAt = new FeuilleDeCalcul(localeAustria,mainWindowAt);//"de_AT"
		AbsoluteLayout mainWindowAbsolutLayoutAt = new AbsoluteLayout();
		mainWindowAbsolutLayoutAt.setWidth("750.0px");//
		mainWindowAbsolutLayoutAt.setHeight("950.0px");//
		ApplicationMenuBar applicationMenuBarAt= new ApplicationMenuBar(localeAustria,mainWindowAt);
		mainWindowAbsolutLayoutAt.addComponent(applicationMenuBarAt.getApplicationMenuBar(), "top:0.0px;left:10.0px;");
		mainWindowAt.setScrollable(true);
		mainWindowAbsolutLayoutAt.addComponent(maFeuilleAt, "top:10.0px;left:10.0px;");		
		mainWindowAt.setContent(mainWindowAbsolutLayoutAt);
		addWindow(mainWindowAt);

		mainWindowIt.setName("it");
		Locale localeItaly = new Locale("it","IT");
		FeuilleDeCalcul maFeuilleIt = new FeuilleDeCalcul(localeItaly,mainWindowIt);//"it_IT"
		AbsoluteLayout mainWindowAbsolutLayoutIt = new AbsoluteLayout();
		mainWindowAbsolutLayoutIt.setWidth("750.0px");//
		mainWindowAbsolutLayoutIt.setHeight("950.0px");//
		ApplicationMenuBar applicationMenuBarIt= new ApplicationMenuBar(localeItaly,mainWindowIt);
		mainWindowAbsolutLayoutIt.addComponent(applicationMenuBarIt.getApplicationMenuBar(), "top:0.0px;left:10.0px;");
		mainWindowIt.setScrollable(true);
		mainWindowAbsolutLayoutIt.addComponent(maFeuilleIt, "top:10.0px;left:10.0px;");		
		mainWindowIt.setContent(mainWindowAbsolutLayoutIt);
		addWindow(mainWindowIt);
		
		mainWindowEs.setName("es");
		Locale localeSpain = new Locale("es","ES");
		FeuilleDeCalcul maFeuilleEs = new FeuilleDeCalcul(localeSpain,mainWindowEs);//"es_ES"
		AbsoluteLayout mainWindowAbsolutLayoutEs = new AbsoluteLayout();
		mainWindowAbsolutLayoutEs.setWidth("750.0px");//
		mainWindowAbsolutLayoutEs.setHeight("950.0px");//
		ApplicationMenuBar applicationMenuBarEs= new ApplicationMenuBar(localeSpain,mainWindowEs);
		mainWindowAbsolutLayoutEs.addComponent(applicationMenuBarEs.getApplicationMenuBar(), "top:0.0px;left:10.0px;");
		mainWindowEs.setScrollable(true);
		mainWindowAbsolutLayoutEs.addComponent(maFeuilleEs, "top:10.0px;left:10.0px;");		
		mainWindowEs.setContent(mainWindowAbsolutLayoutEs);
		addWindow(mainWindowEs);
		
		mainWindowPt.setName("pt");
		Locale localePortugal = new Locale("pt","PT");
		FeuilleDeCalcul maFeuillePt = new FeuilleDeCalcul(localePortugal,mainWindowPt);//"pt_PT"
		AbsoluteLayout mainWindowAbsolutLayoutPt = new AbsoluteLayout();
		mainWindowAbsolutLayoutPt.setWidth("750.0px");//
		mainWindowAbsolutLayoutPt.setHeight("950.0px");//
		ApplicationMenuBar applicationMenuBarPt= new ApplicationMenuBar(localePortugal,mainWindowPt);
		mainWindowAbsolutLayoutPt.addComponent(applicationMenuBarPt.getApplicationMenuBar(), "top:0.0px;left:10.0px;");
		mainWindowPt.setScrollable(true);
		mainWindowAbsolutLayoutPt.addComponent(maFeuillePt, "top:10.0px;left:10.0px;");		
		mainWindowPt.setContent(mainWindowAbsolutLayoutPt);
		addWindow(mainWindowPt);
		
		mainWindowTr.setName("tr");
		Locale localeTurkey = new Locale("tr","TR");
		FeuilleDeCalcul maFeuilleTr = new FeuilleDeCalcul(localeTurkey,mainWindowTr);//"tr_TR"
		AbsoluteLayout mainWindowAbsolutLayoutTr = new AbsoluteLayout();
		mainWindowAbsolutLayoutTr.setWidth("750.0px");//
		mainWindowAbsolutLayoutTr.setHeight("950.0px");//
		ApplicationMenuBar applicationMenuBarTr= new ApplicationMenuBar(localeTurkey,mainWindowTr);
		mainWindowAbsolutLayoutTr.addComponent(applicationMenuBarTr.getApplicationMenuBar(), "top:0.0px;left:10.0px;");
		mainWindowTr.setScrollable(true);
		mainWindowAbsolutLayoutTr.addComponent(maFeuilleTr, "top:10.0px;left:10.0px;");		
		mainWindowTr.setContent(mainWindowAbsolutLayoutTr);
		addWindow(mainWindowTr);
		
		mainWindowBe.setName("be");
		Locale localeBelgium = new Locale("fr","BE");
		FeuilleDeCalcul maFeuilleBe = new FeuilleDeCalcul(localeBelgium,mainWindowBe);//"tr_TR"
		AbsoluteLayout mainWindowAbsolutLayoutBe = new AbsoluteLayout();
		mainWindowAbsolutLayoutBe.setWidth("750.0px");//
		mainWindowAbsolutLayoutBe.setHeight("950.0px");//
		ApplicationMenuBar applicationMenuBarBe= new ApplicationMenuBar(localeBelgium,mainWindowBe);
		mainWindowAbsolutLayoutBe.addComponent(applicationMenuBarBe.getApplicationMenuBar(), "top:0.0px;left:10.0px;");
		mainWindowBe.setScrollable(true);
		mainWindowAbsolutLayoutBe.addComponent(maFeuilleBe, "top:10.0px;left:10.0px;");		
		mainWindowBe.setContent(mainWindowAbsolutLayoutBe);
		addWindow(mainWindowBe);
		
		mainWindowUk.setName("gb");
		Locale localeUnitedKingdom = new Locale("en","GB");
		FeuilleDeCalcul maFeuilleUk = new FeuilleDeCalcul(localeUnitedKingdom,mainWindowBe);//"tr_TR"
		AbsoluteLayout mainWindowAbsolutLayoutUk = new AbsoluteLayout();
		mainWindowAbsolutLayoutUk.setWidth("750.0px");//
		mainWindowAbsolutLayoutUk.setHeight("950.0px");//
		ApplicationMenuBar applicationMenuBarUk= new ApplicationMenuBar(localeUnitedKingdom,mainWindowUk);
		mainWindowAbsolutLayoutUk.addComponent(applicationMenuBarUk.getApplicationMenuBar(), "top:0.0px;left:10.0px;");
		mainWindowUk.setScrollable(true);
		mainWindowAbsolutLayoutUk.addComponent(maFeuilleUk, "top:10.0px;left:10.0px;");		
		mainWindowUk.setContent(mainWindowAbsolutLayoutUk);
		addWindow(mainWindowUk);
		
		//mainWindow.setContent(mainLayout);
		buildMainLayout();
		mainWindow.getContent().setSizeFull();
		//MainPanel.setContent(mainLayout);
		//mainWindow.addComponent(MainPanel);
		mainWindow.setContent(mainLayout);
		//StartWindow startWindow = new StartWindow();
		//mainWindow.addComponent(startWindow);
		
		//Use the locale from the request as default
		//Locale defaultLocal = ((WebApplicationContext)getContext()).getBrowser().getLocale();
		
		//maFeuille.setLocale(defaultLocal);
		mainWindow.setScrollable(true);
		
			
	}
	
	private static void setCurrentLocale(Locale locale){
		currentLocaleValue = locale;
	}
	
	private static Locale getCurrentLocale(){
		return currentLocaleValue;
	}
	private int getWindowTypeNumber(String name, Locale locale){
		int number = 0;
		messages = new Messages("FeuilleDeCalcul",locale);
		
		/*if(name.equalsIgnoreCase(messages.getString("analyseIndividuelWindowName"))){
			return 3;
		}*/
		if(name.equalsIgnoreCase( messages.getString("calculCroiseWindowName"))){
			return 3;
		}
		return number;
	}
	
	// Override the default implementation
	public static SystemMessages getSystemMessages() {
		
	CustomizedSystemMessages systemMessages = new CustomizedSystemMessages();
	systemMessages.setSessionExpiredNotificationEnabled(false);
	systemMessages.setCommunicationErrorNotificationEnabled(false);
		
	return systemMessages;
	}

}
